package com.zb.tcp.rpc.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import org.apache.avro.Protocol;
import org.apache.avro.Schema;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.file.DataFileStream;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericData.Array;
import org.apache.avro.generic.GenericData.Record;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.commons.beanutils.PropertyUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
//import com.flowsea.dtp.dtpserver.entity.MSISDNInfo;

/**
 * Avro搴忓垪鍙锋娊璞＄被锛屽伐鍘傜敓浜vro瀵硅薄鏃剁户鎵�
 * @author qintang
 *
 */
public abstract class AvroSerializeBase {

	
	public static void main(String[] args) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
//		GenericRecord record = AvroSerializeBase.transfer(new MSISDNInfo());
//		Protocol protocol = AvroUtils.getProtocol("D:\\WebWorkspace\\dtpServer\\WebContent\\WEB-INF\\classess\\context\\avro\\validateMSISDN.avro");
//		System.out.println(AvroSerializeBase.transfer(AvroUtils.getProtocolMsgType(protocol), record));
	}
	
	
	
	/**
	 * ʹ��java �෴���ȡschema��ת��ΪAvro record
	 * @param srcObj
	 * @return
	 * @throws IOException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public static GenericRecord transfer(Object srcObj) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Schema schema = AvroUtils.getSchemaFromObjClass(srcObj.getClass());
		Record avroRecord = new Record(schema);
		Field[] ObjFields = srcObj.getClass().getDeclaredFields();
		for(Field field:ObjFields){
			String fieldNm = field.getName();
			avroRecord.put(fieldNm, PropertyUtils.getProperty(srcObj, fieldNm));			
		}
		return avroRecord;
        
	}
	
	
	/**
	 * ��jsonֱ��ת��ΪAvro record
	 * @param avroObjSchema
	 * @param srcObj
	 * @return
	 * @throws IOException
	 */
	public static GenericRecord transfer(Schema avroObjSchema, JSONObject srcObj) throws IOException {  
		 String jsonObjStr=srcObj.toJSONString();
		 DatumReader<GenericRecord> reader=new GenericDatumReader<GenericRecord>(avroObjSchema);
		 Decoder decoder=DecoderFactory.get().jsonDecoder(avroObjSchema,jsonObjStr);
		 GenericRecord retAvroRecord=reader.read(null,decoder);
		 return retAvroRecord;
		  
	}
	
	
	/**
	 * json搴忓垪鍖栦负avro
	 * @param avroObjSchema
	 * @param srcObj
	 * @return
	 * @throws IOException
	 */
	public static GenericRecord transfer(Schema avroObjSchema, Object srcObj) throws IOException {  
		 String jsonObjStr=JSONObject.toJSONString(srcObj);
		 DatumReader<GenericRecord> reader=new GenericDatumReader<GenericRecord>(avroObjSchema);
		 Decoder decoder=DecoderFactory.get().jsonDecoder(avroObjSchema,jsonObjStr);
		 GenericRecord retAvroRecord=reader.read(null,decoder);
		 return retAvroRecord;
		  
	}
	
		
	/**
	 * avro杞洖Pojo
	 * @param avroObjSchema
	 * @param record
	 * @return
	 * @throws IOException 
	 */
	public static Object transfer(GenericRecord record,Class objClass) throws IOException{
		JSONObject json = JSON.parseObject(record.toString());
		return JSONObject.toJavaObject(json, objClass);

	}
	
	
	/**
	 * ͨ��avro record json�ַ���ֱ��ת��JavaObject
	 * @param record
	 * @return
	 * @throws IOException
	 */
	public static Object transfer(GenericRecord record) throws IOException{
		return  JSONObject.parse(record.toString());		
	}
	
	
	/**
	 * pojo鏁扮粍杞垚avro arrays
	 * @param avroObjSchema
	 * @param pojoObj
	 * @param srcObjArray
	 * @return
	 * @throws IOException
	 */
	public static Array<GenericRecord> transfer(Schema avroObjSchema,Object pojoObj,ArrayList<Object> srcObjArray) throws IOException{
		GenericData.Array retAvroArrays = new GenericData.Array(srcObjArray.size(),avroObjSchema);
		for(Object itemObj:srcObjArray){
			GenericRecord avroRecord = transfer(avroObjSchema,itemObj);
			retAvroArrays.add(avroRecord);
		}
		return retAvroArrays;
	}
	
	
	public static ArrayList<Object> transfer(Schema avroObjSchema,GenericData.Array<GenericRecord> avroArrays) throws IOException{
		ArrayList<Object> retObjArrays = new ArrayList<Object>();
		for(GenericRecord avroRecord:avroArrays){
			Object object = transfer(avroObjSchema,avroRecord); 
			retObjArrays.add(object);
		}
		return retObjArrays;
	}
	
	


	


}
